﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestForm
{
    public partial class Form1 : Form
    {
        Process softKeyboard;
        public Form1()
        {
            InitializeComponent();
        }
      
        private void buttonCancell_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if(IDCardCheck.Check(textBoxNumber.Text.Trim()))
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("身份证号码输入有误!请重新输入...");
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Point p0 = new Point(0, 0);
            p0.X -= 40;
            p0.Y += textBoxNumber.Height+5;
            Point t = textBoxNumber.PointToScreen(p0);
            string strParas = string.Format("{0} {1} {2}", t.X, t.Y, this.Handle);
            string strExePath = Path.Combine(Application.StartupPath, "SoftKeyboardSample.exe");
            softKeyboard = Process.Start(strExePath, strParas);
            softKeyboard.EnableRaisingEvents = true;
        }

        private void buttonTest_Click(object sender, EventArgs e)
        {
            if (softKeyboard != null && !softKeyboard.HasExited)
            {
                softKeyboard.CloseMainWindow();
            }
            Point p0 = new Point(0, 0);
            p0.X -= 40;
            p0.Y += textBoxNumber.Height + 5;
            Point t = textBoxNumber.PointToScreen(p0);
            string strParas = string.Format("{0} {1} {2}", t.X, t.Y, this.Handle);
            string strExePath = Path.Combine(Application.StartupPath, "SoftKeyboardSample.exe");
            softKeyboard = Process.Start(strExePath, strParas);
            softKeyboard.EnableRaisingEvents = true;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(softKeyboard != null && !softKeyboard.HasExited)
            {
                softKeyboard.CloseMainWindow();
            }
        }

        public const int USER_MESSAGE_ID = 0x0400;//用户自定义消息的开始数值
        protected override void DefWndProc(ref System.Windows.Forms.Message m)
        {
            switch (m.Msg)
            {
                case USER_MESSAGE_ID:
                    if (IDCardCheck.Check(textBoxNumber.Text.Trim()))
                    {
                        m.Result = (IntPtr)0x00;
                        this.DialogResult = System.Windows.Forms.DialogResult.OK;
                        this.Close();
                    }
                    else
                    {
                        m.Result = (IntPtr)0xff;
                        MessageBox.Show("身份证号码输入有误!请重新输入...");
                    }
                    break;
                default:
                    base.DefWndProc(ref m);//一定要调用基类函数，以便系统处理其它消息。
                    break;
            }
        }
    }
}
